#version 460


layout(points) in;
layout(triangle_strip, max_vertices=64) out;




#include "../lib/project_matrix.glsl"
#include "../lib/vertex_in.glsl"




//图元 顶点偏移和顶点数量 
layout(std430, column_major, binding = 5) readonly buffer U5_Matrix {
	uvec2 m_VertOffset_Num[];
};



in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 1) in VS_GS_VERTEX {
	uint vInstance;
}vertex_in[];


layout(location = 0) out vec2 vUV;
layout(location = 1) out flat uint gInstance;
layout(location = 2) out flat float gap;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};
//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};
void main(void){
	mat3x2 mat = m_LP_SD[vertex_in[0].vInstance];
	uvec2 VertOffsetNum = m_VertOffset_Num[vertex_in[0].vInstance];

	
	uint vertOffset = VertOffsetNum.x;
	uint num = VertOffsetNum.y - VertOffsetNum.x;
	if(num >= 3) num -= 2;
	for(uint i=0; i<num; ++i){
		
		vec4 v = vec4(m_Vertex[VertOffsetNum.x].coord, 0, 1);
		vec2 Weights = m_Vertex[VertOffsetNum.x].weight;
		
		v.xy = (v.xy * mat[2].x) + ((mat[1]-mat[2].x) * Weights.xy);
		vUV = v.xy;
		v.xy += mat[0];
		v.xy *= m_Scale;

		
		//vUV.y /= gap;

		v.xy += m_Loc;
		
		//vUV.y *= mat[1].y;
		
		
		gl_Position = mat_ProjecView * v;
		gl_PointSize = mat[2].y;
		

		gInstance = vertex_in[0].vInstance;
		gap = mat[2].y*2;
		EmitVertex();

		for(uint j=1; j<3; ++j){
			v = vec4(m_Vertex[vertOffset + j].coord, 0, 1);
			Weights = m_Vertex[vertOffset + j].weight;

			v.xy = (v.xy * mat[2].x) + ((mat[1]-mat[2].x) * Weights.xy);
			vUV = v.xy;
			v.xy += mat[0];
			v.xy *= m_Scale;

			

			v.xy += m_Loc;
			//vUV.y *= mat[1].y;
			

			gl_Position = mat_ProjecView * v;
			gl_PointSize = mat[2].y;
			
			
			//实例ID 行高度
			gInstance = vertex_in[0].vInstance;
			gap = mat[2].y*2;
			EmitVertex();
		}
		
		EndPrimitive();
		++vertOffset;
	}
	
	

}



 /*gl_Position = mat_ProjecView * vec4(m_Loc.x, m_Loc.y,0,1);
	gl_PointSize = 1.0;
	gInstance = vertex_in[0].vInstance;
	gap = mat[2].y;
	EmitVertex();
	gl_Position = mat_ProjecView * vec4(m_Loc.x+1000, m_Loc.y, 0,1);
	gl_PointSize = 1.0;
	gInstance = vertex_in[0].vInstance;
	gap = mat[2].y;
	EmitVertex();
	gl_Position = mat_ProjecView * vec4(m_Loc.x+1000, m_Loc.y+1000,0,1);
	gl_PointSize = 1.0;
	gInstance = vertex_in[0].vInstance;
	gap = mat[2].y;
	EmitVertex();
	gl_Position = mat_ProjecView * vec4(m_Loc.x+0, m_Loc.y+1000,0,1);
	gl_PointSize = 1.0;
	gInstance = vertex_in[0].vInstance;
	gap = mat[2].y;
	EmitVertex();
	EndPrimitive();
	return;*/
